home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / nfbtr731.zip / NFBPATCH.C < prev    next >
C/C++ Source or Header  |  1995-01-24  |  8KB  |  288 lines

  1. /**************************************************************************
  2. NAME: Brian Buhrow
  3. DATE: March 23, 1993
  4. PURPOSE: The purpose of this file is to hold patches for the nfbtrans
  5. braille translation software so that it will run under Unix relatively
  6. invisibly.
  7. Documentation will go with each function.
  8. **************************************************************************/
  9. /*$Log: nfbpatch.c,v $
  10.  * Revision 1.1  1993/05/18  06:12:23  buhrow
  11.  * Initial revision
  12.  **/
  13. #ifdef unix /*Only use if we're compiling under Unix*/
  14.  
  15. #ifndef lint /*We don't want it in lint output*/
  16. static char RCSID[]="$Id: nfbpatch.c,v 1.1 1993/05/18 06:12:23 buhrow Exp buhrow $";
  17. #endif /*lint*/
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/types.h>
  22. #include <sys/termios.h>
  23. #include <sys/stat.h>
  24. #include <sys/time.h>
  25. #include <unistd.h>
  26.  
  27. #define CASE_SHIFT 32 /*diference between upper and lower case*/
  28. #define GLOBTABFIL "/usr/local/lib/braille.tab" /*Name of global translation table*/
  29. #define TABLENAME "braille.tab"
  30. #define SYSVR4 /*use with gcc and solaris 2.1 see getpgrp.
  31. *Also defines usleep.  Wasn't able to link with /usr/ucblib/libucb.a
  32. *Really no need for delay anyway*/
  33. #ifdef ultrix
  34. #define ECHOCTL TCTLECH
  35. #endif
  36.  
  37. struct termios original_stdin;
  38.  
  39. char *strlwr(char *string)
  40. /*Takes the indicated string and changes uppercase letters to lowercase
  41. ones.  All other characters are unaffected.  Returns a pointer to the
  42. string.*/
  43. {
  44.     int i;
  45.  
  46.     for (i = 0;i < strlen(string);i ++) /*for the entire string*/
  47.     {
  48.         if ((string[i] >= 'A') && (string[i] <= 'Z'))
  49.         {
  50.             string[i] = string[i] + CASE_SHIFT;
  51.         }
  52.     }
  53.     return(string);
  54. }/*strlwr*/
  55.  
  56. char *strupr(char *string)
  57. /*Takes the indicated string and changes lowercase letters to uppercase
  58. ones.  All other characters are unaffected.  Returns a pointer to the
  59. string.*/
  60. {
  61.     int i;
  62.  
  63.     for (i = 0;i < strlen(string);i ++) /*for the entire string*/
  64.     {
  65.         if ((string[i] >= 'a') && (string[i] <= 'z'))
  66.         {
  67.             string[i] = string[i] - CASE_SHIFT;
  68.         }
  69.     }
  70.     return(string);
  71. }/*strupr*/
  72.  
  73. void strnset(char *string,char c,short n)
  74. /*sets the first n or length of string characters to c whichever is less*/
  75. {
  76.     short i;
  77.     for(i=0; (i < n && string[i] != '\0'); i++)
  78.     string[i] = c;
  79. }/*strnset*/
  80.  
  81. int inforeground()
  82. /*Returns 1 if the program is running in the foreground, 0 otherwise.*/
  83. {
  84.     int i;
  85.  
  86.     i = tcgetpgrp(0); /*Use stdin as the file descriptor*/
  87.     #ifdef SYSVR4
  88.     if (i == getpgrp()) return(1);
  89. #else
  90.     if (i == getpgrp(0)) return(1);
  91. #endif /*sysv*/
  92.     else return(0);
  93. }/*inforeground*/
  94.  
  95. int getch()
  96. /*Reads a character from standard input.  It doesn't require a carriage
  97. return. No echo to stdout.*/
  98. {
  99.   tcflag_t oldval; /* Value of the terminal flags */
  100.   tcflag_t mask = ECHO | ECHOCTL | ICANON;    /* bits to turn off
  101.                          * temporarily */
  102.   char charin;       /* input character */
  103.   cc_t oldmin;       /* save original min */
  104.   struct termios termset;    /* What is our terminal set to */
  105.  
  106.   tcgetattr(0, &termset);
  107.   oldval = termset.c_lflag;
  108.   oldmin = termset.c_cc[VMIN];
  109.   termset.c_lflag |= mask;    /* turn canonical input and echo on */
  110.   termset.c_lflag ^= mask;    /* turn them off now */
  111.   termset.c_cc[VMIN] = 1;    /* fgetc will return after first character */
  112.   tcsetattr(0,TCSANOW , &termset);
  113.   charin = (char) fgetc(stdin);
  114.   termset.c_lflag = oldval;    /* restore original settings */
  115.   termset.c_cc[VMIN] = oldmin;
  116.   tcsetattr(0, TCSANOW, &termset);
  117.   fprintf(stderr," \010");
  118.   return ((int) charin);
  119. }           /* getch */
  120.  
  121. int getche()
  122. /*Reads a character from standard input, ecoes to standard output, doesn't
  123. require a carriage return.*/
  124. {
  125.     tcflag_t oldval; /*Value of the terminal flags*/
  126.     tcflag_t mask = ICANON | ECHOCTL;
  127.     cc_t oldmin;/*save original min*/
  128.     char charin; /*input character*/
  129.     struct termios termset; /*What is our terminal set to*/
  130.  
  131.     tcgetattr(0, &termset);
  132.     oldval = termset.c_lflag;
  133.     oldmin = termset.c_cc[VMIN];
  134.     /*turn off canonical and echo control*/
  135.     termset.c_lflag |= mask; /*turn both on*/
  136.     termset.c_lflag ^= mask; /*turn both off*/
  137.     termset.c_cc[VMIN] = 1;
  138.   tcsetattr(0, TCSANOW, &termset);
  139.     charin = (char)fgetc(stdin) ;
  140.     termset.c_lflag = oldval; /*restore original settings*/
  141.     termset.c_cc[VMIN] = oldmin;/*restore original min*/
  142.   tcsetattr(0, TCSANOW, &termset);
  143.     return((int)charin);
  144. }
  145.  
  146. void unbuf_stdin()
  147. {
  148.     tcflag_t mask = ICANON; /*bits to turn off temporarily*/
  149.     struct termios termset; /*What is our terminal set to*/
  150.  
  151.     tcgetattr(0, &termset);
  152.     termset.c_lflag |= mask; /*turn canonical input on*/
  153.     termset.c_lflag ^= mask; /*turn them off now*/
  154.     termset.c_cc[VMIN] = 1;/*fgetc will return after first character*/
  155.     tcsetattr(0, TCSANOW, &termset);
  156. }/*unbuf_stdin*/
  157.  
  158. void save_stdin()
  159. {
  160.     tcgetattr(0, &original_stdin);
  161. }/*save_stdin*/
  162.  
  163. void restore_stdin()
  164. {
  165.     tcsetattr(0, TCSANOW, &original_stdin);
  166. }/*restore_stdin*/
  167.  
  168. long filelength(descriptor)
  169.     int descriptor;
  170. /*Returns the length of the associated file in bytes.  If the descriptor is
  171. bad, returns -1 and errno is set to the proper number.*/
  172. {
  173.     struct stat finfo; /*File information here*/
  174.     if (fstat(descriptor, &finfo))
  175.     {
  176.         return(EOF);
  177.     }
  178.     return(finfo.st_size);
  179. }/*filelength*/
  180.  
  181. #ifdef SYSVR4
  182. void usleep(usec)
  183. int usec;
  184. {
  185.     int i;
  186.     for(i=0; i<usec; i++)
  187.     i = i;
  188. }/*usleep*/
  189. #endif
  190.  
  191. void beep(count)
  192.     int count;
  193. /*Beeps the terminal bell the specified number of times.*/
  194. {
  195.     static char tbuf[1024]; /*terminal buffer*/
  196.     static have_ent = 0; /*We want to know if we need to get terminal entry*/
  197.     static char *blstr; /*pointer for bell string*/
  198.     static char *ptr = tbuf;
  199.     int i;
  200.  
  201.     if (!have_ent)
  202.     {
  203.         if ((tgetent(&tbuf, (char *)getenv("TERM"))) == 1)
  204.         {
  205.             blstr = (char *)tgetstr("bl", &ptr);
  206.             if (blstr) have_ent = 1;
  207.         }
  208.     }
  209.     if ((have_ent) && (inforeground()))
  210.     {
  211.         if (isatty(fileno(stdout)))
  212.         {
  213.             for (i = 0;i < count;i ++) /*beep count number of times*/
  214.             {
  215.                 fprintf(stdout,"%s",blstr);
  216.             }
  217.         }
  218.     }
  219.     return;
  220. }
  221.  
  222. int kbhit()
  223. /*This function emulates the kbhit function under  MS-DOS.  If the standard
  224. input is ready for reading, then a 1 is returned.  Otherwise, a 0 is
  225. returned, indicating that no input is there.  If this process is not
  226. running in the foreground, then this function returns 0.*/
  227. {
  228.     int i;
  229.     struct timeval timeout; /*For polling purposes*/
  230.     fd_set readfds; /*Reading flags parm for select*/
  231.  
  232.     if (!inforeground()) return(0);
  233.     memset(&timeout, 0, sizeof(struct timeval)); /*Zero out the timer*/
  234.     FD_ZERO(&readfds);
  235.     FD_SET(0, &readfds); /*set the descriptors we're interested in*/
  236.        return(select(1, &readfds, NULL, NULL, &timeout) > 0);
  237. }/*kbhit*/
  238.  
  239. void delay_unix(period)
  240.     unsigned int period;
  241. /*This routine pauses the program for the specified number of milliseconds
  242. before returning.*/
  243. {
  244.     long microperiod; /*amount of time, in microseconds*/
  245.  
  246.     microperiod = period * 100; /*Convert milliseconds to microseconds*/
  247.     usleep((unsigned)microperiod);
  248.     return;
  249. }/*delay_unix*/
  250.  
  251. void get_tablename(char *string)
  252. /*Takes the value pointed to by string and copies the pathname of the table
  253. file into that string.    The algorithm used is as follows:
  254. (1) If the value in the environment variable "NFBTAB" points to a readable
  255. file, then that file is used.
  256. (2) Else, if the file "/usr/local/lib/braille.tab" is readable, then that
  257. is used.
  258. (3) else, if braille.tab exists in current directory it is used.
  259. Else, an error message is generated and the program exits.*/
  260. {
  261.     char *strptr; /*pointer to our file name*/
  262.  
  263.     strptr = (char *)getenv("NFBTAB");
  264.     if (strptr)
  265.     {
  266.         if (!access(strptr, R_OK))
  267.         {
  268.             strcpy(string, strptr);
  269.             return;
  270.         }
  271.     }
  272.     if (!access(GLOBTABFIL, R_OK))
  273.     {
  274.         strcpy(string, GLOBTABFIL);
  275.         return;
  276.     }
  277.     if (!access(TABLENAME, R_OK))
  278.     {
  279.         strcpy(string,TABLENAME);
  280.         return;
  281.     }
  282.     fprintf(stderr,"get_tablename: %s: ",GLOBTABFIL);
  283.     perror("");
  284.     exit(4);
  285. }
  286.  
  287. #endif /*unix*/
  288.